<template>
  <div id="app">
    <el-upload
      class="avatar-uploader"
      action="#"
      list-type="picture-card"
      :file-list="file1"
      :on-preview="preview"
      :limit="number"
      name="avatar"
      :on-remove="removeFile"
      :on-change="OnChange"
      :class="{ disabled:file1.length >= number }"
      :before-upload="beforeUpload"
      :http-request="upload"
    >

      <i class="el-icon-plus" />
    </el-upload>
    <el-progress v-if="showprogress" style="width:180px" :percentage="percent" />
    <el-dialog :visible.sync="dialogVisible">
      <el-row type="flex" justify="center"> <img :src="imageUrl" alt="" style="width:100%;hight:100%;"></el-row>
    </el-dialog>

  </div>
</template>
<script>
import COS from 'cos-js-sdk-v5'
const cos = new COS({ SecretId: 'AKID97rOfhV4PWO2QZUBcnqc7LL7vfAmCYiX', SecretKey: 'g5OULyLYVDgsrdAUdZHnCRSFpERO3d5S' })
console.log(cos)

export default {
  name: 'ImageUpload',
  data() {
    return {
      showprogress: false,
      percent: 0,
      number: 2,
      dialogVisible: false,
      imageUrl: '',
      file1: []
    }
  },
  methods: {
    preview(file) {
      this.dialogVisible = true
      this.imageUrl = file.url
    },
    removeFile(file) {
      this.file1 = this.file1.filter(item => { item.uid !== file.uid })
    },
    OnChange(file, filelist) {
      this.file1 = filelist
    },
    beforeUpload(file) {
      const types = ['image/jpeg', 'image/jpeg', 'image/bmp', 'image/png']
      if (!types.includes(file.type)) {
        this.$message.error('上传失败,请上传jpeg/jpeg/bmp/png格式的文件')
        return false
      }
      const filesize = 2 * 1024 * 1024
      if (file.size > filesize) {
        this.$message.error('上传失败,请上传2M以下的图片')
        return false
      }
    },
    upload(params) {
      this.showprogress = true
      cos.putObject({
        Bucket: '1316936133-1316936133', /* 填入您自己的存储桶，必须字段 */
        Region: 'ap-nanjing', /* 存储桶所在地域，例如ap-beijing，必须字段 */
        Key: params.file.name, /* 存储在桶里的对象键（例如1.jpg，a/b/test.txt），必须字段 */
        Body: params.file, /* 必须，上传文件对象，可以是input[type="file"]标签选择本地文件后得到的file对象 */
        onProgress: (progressData) => {
          console.log(JSON.stringify(progressData))
          this.percent = +(progressData.percent * 100).toFixed(2)
        }
      }, (err, data) => {
        if (err) return
        this.file1.forEach(item => {
          if (item.uid === params.file.uid) {
            item.url = `http://${data.Location}`
            // 做标记 有则表示url可用
            item.upload = true
          }
        })
        setTimeout(() => {
          this.showprogress = false
          this.percent = 0
        }, 1000)
      })
    }
  }

}
</script>

<style lang="scss" >
.disabled{
    .el-upload.el-upload--picture-card{
        display: none
      }}

      .avatar-uploader.el-upload {
      border: 1px dashed #d9d9d9;
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
      }
      .avatar-uploader .el-upload:hover {
      border-color: #409EFF;
      }
      .avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 178px;
      height: 178px;
      line-height: 178px;
      text-align: center;
      }
      .avatar {
      width: 178px;
      height: 178px;
      display: block;
      }
</style>

